<template>
	<view class="search-box">
		<uni-search-bar @input="input" @blur="mouseout" @focus="focus" :radius="100" cancelButton="none" placeholder="请输入搜索内容" clearButton="always"></uni-search-bar>
		<!-- 搜索建议 -->
		<view class="sugg-list" v-if="showSuggList">
			<view class="sugg-item" v-for="(item, i) in searchResults" :key="i" @click="gotoGoodDetail(item.goods_id)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons class="righticon" type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="searchHistory-container" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clearHistory"></uni-icons>
			</view>
			<view class="history-item-container">
				<view class="history-item" v-for="(item, i) in historyList" :key="i" @click="gotoGoodsList(item)">{{item}}</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				kw: '',
				searchResults: [],
				showSuggList: false,
				historyList: []
			};
		},
		methods: {
			input(e) {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.kw = e
					this.getSearchResults()
				}, 500)
			},
			focus() {
				if(this.kw !== '' && this.searchResults.length !== 0) this.showSuggList = true
			},
			async getSearchResults() {
				if(this.kw.trim() === '') return
				const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', {query: this.kw})
				if(res.meta.status !== 200) return uni.$showMsg()
				console.log(res)
				this.searchResults = res.message
				if(this.searchResults.length === 0) this.showSuggList = false
				this.focus()
				this.saveSearchHistory()
			},
			gotoGoodDetail(goods_id) {
				uni.navigateTo({
					url: "/subpkg/goods_detail/goods_detail?gid=" + goods_id
				})
			},
			mouseout() {
				this.showSuggList = false
			},
			saveSearchHistory() {
				// 如果没有搜索历史就直接加入历史
				if(this.historyList.length === 0) {
					this.historyList.push(this.kw)
					return
				}
				// 如果有搜索历史，就查重，如果有重复的就把以前的删除
				const set = new Set(this.historyList.reverse())
				for(let i=0; i<this.historyList.length; i++) {
					if(this.historyList[i] === this.kw) {
						set.delete(this.kw)
						break
					}
				}
				// 在首位插入新的搜索词
				this.historyList = Array.from(set)
				this.historyList.unshift(this.kw)
				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			clearHistory() {
				this.historyList = []
				uni.setStorageSync('kw', JSON.stringify(this.historyList))
			},
			gotoGoodsList(kw) {
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + kw
				})
			}
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		}
	}
</script>

<style lang="scss">
.search-box {
	position: sticky;
	top: 0;
	z-index: 999;
}
.sugg-list {
	font-size: 12px;
	padding: 5px 10px;
	.sugg-item {
		border-bottom: 1px solid #efefef;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: 10px 0;
		.goods-name {
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}
		.righticon {
		}
	}
}
.searchHistory-container {
	padding: 5px 10px;
	.history-title {
		font-size: 15px;
		display: flex;
		justify-content: space-between;
		margin-bottom: 5px;
	}
	.history-item-container {
		display: flex;
		flex-wrap: wrap;
		gap: 5px;
		.history-item {
			background-color: #D8D8D8;
			font-size: 12px;
			margin-bottom: 5px;
			border-radius: 3px;
			padding: 3px 10px;
		}
	}
}
</style>
